<?php
/**
 *  经销商采购订单
*/

namespace app\api\model;

use think\facade\Lang;

class WholesalOrder extends BaseModel
{
    protected $name = 'order';
    protected $append = ['status_text','pay_status_text','pay_terms_text'];
    protected $attributes = [
        'status' => [
            10 => 'status.pendingPayment',     //待支付
            20 => 'status.inReview',  // 审核中
            21 => 'status.pendingPayment',   // 待支付
            70 => 'status.cancelled',  // 已取消
            100 => 'status.completed',  // 已完成
        ],
        'pay_terms' => [
            0 => 'payTerms.fullPayment',
            1 => 'payTerms.advanceCharge'
        ],
        'pay_status' => [
            1 => 'payStatus.unpaid',
            2 => 'payStatus.audit',
            3 => 'payStatus.paid',
            4 => 'payStatus.partialPayment',
        ],
        'reviewer_status' => [
            1 => 'reviewerStatus.noNeedReview',
            2 => 'reviewerStatus.partialPayment',
            3 => 'reviewerStatus.approved'
        ],
        'pay_method' => [
            1 => 'payMethod.cryptoPay',
            2 => 'payMethod.coinbasePay',
            3 => 'payMethod.wireTransfer',
        ]

    ];

    /**
     *  订单状态
     */
    public function getStatusTextAttr($value,$data)
    {
        $statusList = $this->attributes;
        if (isset($statusList['status'])){
            return Lang::get($statusList['status'][$data['status']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }

    /**
     *  是否全款
     */
    public function getPayTermsTextAttr($value,$data)
    {
        $statusList = $this->attributes;
        if (isset($statusList['pay_terms'])){
            return Lang::get($statusList['pay_terms'][$data['pay_terms']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }

    /**
     *  支付状态
     */
    public function getPayStatusTextAttr($value,$data)
    {
        if(!isset($data['pay_status'])){
            return Lang::get('order.orderStatusError');
        }

        $statusList = $this->attributes;
        if (isset($statusList['pay_status'])){
            return Lang::get($statusList['pay_status'][$data['pay_status']]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }



    /**
     *  tax_fee
    */
    public function getTaxFeeAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }


    /**
     *  order_amount
     */
    public function getOrderAmountAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }

    /**
     *  order_pay_amount
     */
    public function getOrderPayAmountAttr($value)
    {
        if(is_null($value)){
            return '';
        } else {
            return $value;
        }
    }


    /**
     *  关联品牌
     */
    public function item()
    {
        return $this->hasMany('WholesalOrderItem', 'order_id','id');
    }


    /**
     *  过去订单状态
     */
    public function getFieldEnum($field)
    {
        if ('ALL' === strtoupper($field)) {
            return [
                'pay_terms' => $this->attributes['pay_terms'],
                'pay_status' => $this->attributes['pay_status'],
                'status' => $this->attributes['status'],
                'pay_method' => $this->attributes['pay_method'],
                'reviewer_status' => $this->attributes['reviewer_status']
            ];
        } else {
            return [$field => $this->$field];
        }
    }


    /**
     *  支付记录
     */
    public function paymentRecord()
    {
        return $this->hasMany(OrderPayment::class,'order_id','id');
    }


    /**
     *  获取订单状态
    */
    public function getStatusText($status) {
        $statusList = $this->attributes;
        if (isset($statusList['status'])){
            return Lang::get($statusList['status'][$status]);
        } else {
            return Lang::get('order.orderStatusError');
        }
    }



}
